import Vue from 'vue'
import Router from 'vue-router'
import Vuex from 'vuex'

Vue.use(Router)
Vue.use(Vuex)

/* Layout */
import Layout from '@/layout'
import { AppMain } from '@/layout/components'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: 'Dashboard', icon: 'dashboard' }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/componentshow',
    component: Layout,
    redirect: '/componentShow/index',
    name: 'componentshow',
    meta: {
      title: '组件展示',
      icon: 'ali-yonghu',
      roles: ['administrator', 'webmaster']
    },
    alwaysShow: false,
    children: [
      {
        path: '/componentShow/index',
        name: 'componentshow',
        component: () => import('@/views/componentShow/index'),
        meta: { title: '组件展示', icon: 'form' },
        roles: ['administrator', 'webmaster']
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    redirect: '/user/index',
    name: 'sysUser',
    meta: {
      title: '用户管理',
      icon: 'ali-yonghu',
      roles: ['administrator', 'webmaster']
    },
    alwaysShow: false,
    children: [
      {
        path: 'index',
        name: 'sysUserList',
        component: () => import('@/views/sysUser/index'),
        meta: { title: '用户列表', icon: 'ali-yonghu' }
      },
      {
        path: 'saveUser',
        name: 'saveUser',
        component: () => import('@/views/sysUser/form'),
        meta: { title: '保存用户' },
        hidden: true
      },
      {
        path: 'editUser',
        name: 'editUser',
        component: () => import('@/views/sysUser/form'),
        meta: { title: '编辑用户' },
        hidden: true
      },
      {
        path: 'detail',
        name: 'detailUser',
        component: () => import('@/views/sysUser/form'),
        meta: { title: '用户详情' },
        hidden: true
      }
    ]
  },
  {
    path: '/permission',
    name: 'permission',
    meta: {
      title: '权限管理',
      icon: 'ali-juesequanxian',
      roles: ['administrator', 'webmaster']
    },
    redirect: '/permission/role',
    component: Layout,
    alwaysShow: true,
    children: [
      {
        path: 'role',
        name: 'role',
        component: () => import('@/views/sysRole/index'),
        meta: { title: '角色列表', icon: 'ali-juese' }
      },
      {
        path: 'userRole',
        name: 'userRole',
        component: () => import('@/views/sysUserRole/index'),
        meta: { title: '用户角色', icon: 'ali-yonghujuese' }
      },
      {
        path: 'menu',
        name: 'menu',
        component: () => import('@/views/sysMenu/index'),
        meta: { title: '菜单列表', icon: 'ali-caidan' }
      },
      {
        path: 'roleMenu',
        name: 'roleMenu',
        component: () => import('@/views/sysRoleMenu/index'),
        meta: { title: '角色菜单', icon: 'ali-juesecaidan' }
      }
    ]
  },
  {
    path: '/regions',
    component: Layout,
    redirect: '/regions/index',
    name: 'sysProvinceRegions',
    meta: {
      title: '省市区管理',
      icon: 'ali-city',
      roles: ['administrator', 'webmaster']
    },
    alwaysShow: true,
    children: [
      {
        path: 'index',
        name: 'sysProvinceRegionsList',
        component: () => import('@/views/provinceRegions/index'),
        meta: { title: '省市区列表', icon: 'form' }
      },
      {
        path: 'tree',
        name: 'sysProvinceRegionsTree',
        component: () => import('@/views/provinceRegions/indexTree'),
        meta: { title: '树状列表', icon: 'form' }
      }
    ]
  },
  {
    path: '/favorite',
    component: Layout,
    redirect: '/favorite/favoriteList',
    name: 'favorite',
    meta: { title: '收藏夹管理', icon: 'ali-shoucangjia', roles: ['administrator', 'webmaster'] },
    alwaysShow: true,
    children: [
      {
        path: 'favoriteList',
        name: 'favoriteList',
        component: () => import('@/views/favorite/index'),
        meta: { title: '收藏夹列表', icon: 'ali-shoucangjia' }
      },
      {
        path: 'collect',
        name: 'collect',
        component: () => import('@/views/collect/index'),
        meta: { title: '收藏列表', icon: 'ali-shoucang' }
      },
      {
        path: 'favoriteCollect',
        name: 'favoriteCollect',
        component: () => import('@/views/favoriteCollect/index'),
        meta: { title: '订阅列表', icon: 'ali-dingyue' }
      }
    ]
  },
  {
    path: '/companyDept',
    component: Layout,
    redirect: '/companyDept/company',
    name: 'companyDept',
    meta: { title: '公司管理', icon: 'ali-gongsi', roles: ['administrator', 'webmaster'] },
    alwaysShow: true,
    children: [
      {
        path: 'company',
        name: 'company',
        component: () => import('@/views/company/index'),
        meta: { title: '公司列表', icon: 'ali-gongsi' }
      },
      {
        path: 'dept',
        name: 'dept',
        component: () => import('@/views/companyDept/index'),
        meta: { title: '部门列表', icon: 'ali-gongsi' }
      }
    ]
  },
  {
    path: '/question',
    component: Layout,
    redirect: '/question/interview',
    name: 'question',
    meta: { title: '问题管理', icon: 'ali-jieda' },
    alwaysShow: true,
    children: [
      {
        path: 'interview',
        name: 'interview',
        component: () => import('@/views/interviewQuestion/index'),
        meta: { title: '问题列表', icon: 'ali-jieda' }
      },
      {
        path: 'refactor',
        name: 'refactorQuestionView',
        component: () => import('@/views/interviewQuestion/view'),
        meta: { title: '重构问题列表', icon: 'ali-jieda' }
      },
      {
        path: 'details/:id',
        name: 'questionDetails',
        component: () => import('@/views/interviewQuestion/details'),
        meta: { title: '面试问题详情', icon: 'ali-huida' },
        hidden: true
      },
      {
        path: '/answer',
        component: AppMain,
        meta: { title: '回答列表', icon: 'ali-huida', roles: ['administrator', 'webmaster'] },
        alwaysShow: false,
        children: [
          {
            path: 'list',
            name: 'list',
            component: () => import('@/views/questionAnswer/index'),
            meta: { title: '回答列表', icon: 'ali-huida' }
          },
          {
            path: 'save',
            name: 'save',
            component: () => import('@/views/questionAnswer/form'),
            meta: { title: '新增答案', icon: 'ali-huida' },
            hidden: true
          }
        ]
      },
      {
        path: 'tags',
        name: 'tags',
        component: () => import('@/views/questionLabel/index'),
        meta: { title: '标签列表', icon: 'ali-biaoqian', roles: ['administrator', 'webmaster'] }
      },
      {
        path: 'useful',
        name: 'useful',
        component: () => import('@/views/answerUseful/index'),
        meta: { title: '有用列表', icon: 'ali-dianzan', roles: ['administrator', 'webmaster'] }
      },
      {
        path: 'like',
        name: 'like',
        component: () => import('@/views/likeRecords/index'),
        meta: { title: '点赞列表', icon: 'ali-like', roles: ['administrator', 'webmaster'] }
      },
      {
        path: 'meet',
        name: 'meet',
        component: () => import('@/views/meetRecords/index'),
        meta: { title: '遇见列表', icon: 'eye', roles: ['administrator', 'webmaster'] }
      }
    ]
  },
  {
    path: '/review',
    component: Layout,
    redirect: '/review/list',
    name: 'review',
    meta: { title: '评论管理', icon: 'ali-pinglun' },
    alwaysShow: false,
    children: [
      {
        path: 'list',
        name: 'reviewList',
        component: () => import('@/views/review/index'),
        meta: { title: '评论列表', icon: 'ali-pinglun' }
      }
    ]
  },
  {
    path: '/sys',
    component: Layout,
    redirect: '/sys/file',
    name: 'sys',
    meta: { title: '系统管理', icon: 'ali-wjz', roles: ['administrator', 'webmaster'] },
    alwaysShow: true,
    children: [
      {
        path: 'file',
        name: 'file',
        component: () => import('@/views/fileOss/index'),
        meta: { title: '文件列表', icon: 'ali-file' }
      },
      {
        path: 'log',
        name: 'log',
        component: () => import('@/views/sysOperationLog/index'),
        meta: { title: '操作日志', icon: 'ali-rizhi' }
      },
      {
        path: 'report',
        name: 'report',
        component: () => import('@/views/reportInfo/index'),
        meta: { title: '举报列表', icon: 'ali-jubao' }
      },
      {
        path: 'siteMail',
        name: 'siteMail',
        component: () => import('@/views/siteMail/index'),
        meta: { title: '站内信列表', icon: 'ali-zhanneixin' }
      }
    ]
  },
  {
    path: 'external-link',
    component: Layout,
    meta: { title: 'External Link', icon: 'link' },
    children: [
      {
        path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
        meta: { title: 'External Link', icon: 'link' }
      },
      {
        path: 'https://fzxywl.cn',
        meta: { title: '止语博客', icon: 'link' }
      },
      {
        path: 'https://lesscoding.net',
        meta: { title: 'Lesscoding', icon: 'link' }
      },
      {
        path: 'http://base.lesscoding.net',
        meta: { title: '代码生成器', icon: 'link' }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  })

const router = createRouter()

router.beforeEach((to, from, next) => {
  sessionStorage.setItem('lastFrom', sessionStorage.getItem('from'))
  sessionStorage.setItem('from', from.fullPath)
  next()
})

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
